Now let me summarise it and show a table of the variables.
Code
summary(kids)
state variable year raw
Length:23460 Length:23460 Min. :1997 Min. : -60139
Class :character Class :character 1st Qu.:2002 1st Qu.: 71985
Mode :character Mode :character Median :2006 Median : 252002
Mean :2006 Mean : 1181359
3rd Qu.:2011 3rd Qu.: 836324
Max. :2016 Max. :83666088
NA's :102
inf_adj inf_adj_perchild
Min. : -60799 Min. :-0.01361
1st Qu.: 85876 1st Qu.: 0.12456
Median : 298778 Median : 0.32757
Mean : 1359983 Mean : 0.91448
3rd Qu.: 985049 3rd Qu.: 0.83362
Max. :84584960 Max. :20.27326
NA's :102 NA's :102
It is very tidy. It is probably better shown after a pivot. 50 states, the District of Columbia, and 20 years gives us 1,020 observations. Let me show it wide.
I recently came across a geofacet for R. I want to use it to plot a little bit of this data. If you want to get a head start, try install.packages("geofacet", dependencies=TRUE). You can google geofacet to get an idea of what a geofacet plot is. I will build one on the fly using a couple of tidy tools: filter, mutate, and joins and then put it together.
State.Temp <- state_ranks %>%filter(variable=="education")Final.Data <-left_join(kids, State.Temp, by =c('state'='name')) %>%filter(variable.x =="PK12ed")ggplot(Final.Data, aes(x=year, y=inf_adj_perchild, color = inf_adj_perchild)) +geom_line() +theme_void() +scale_color_viridis_c() +facet_geo(~ state.y) +labs(title="Pre-K through 12 Education Spending per Child", subtitle="Inflation adjusted", color="Spend per Child") -> My.PlotMy.Plot
Getting Real Fancy
gganimate makes animation an easy addition to basic ggplots. The same developer is also responsible for the very accessible patchwork for combining multiple ggplots. I will use the ability to reveal a transition in this animation.
Grolemund, Garrett, and Hadley Wickham. 2011. “Dates and Times Made Easy with lubridate.”Journal of Statistical Software 40 (3): 1–25. https://www.jstatsoft.org/v40/i03/.
Wickham, Hadley, Mara Averick, Jennifer Bryan, Winston Chang, Lucy D’Agostino McGowan, Romain François, Garrett Grolemund, et al. 2019. “Welcome to the tidyverse.”Journal of Open Source Software 4 (43): 1686. https://doi.org/10.21105/joss.01686.
Wickham, Hadley, Winston Chang, Lionel Henry, Thomas Lin Pedersen, Kohske Takahashi, Claus Wilke, Kara Woo, Hiroaki Yutani, and Dewey Dunnington. 2023. Ggplot2: Create Elegant Data Visualisations Using the Grammar of Graphics. https://ggplot2.tidyverse.org.
Wickham, Hadley, Romain François, Lionel Henry, Kirill Müller, and Davis Vaughan. 2023. Dplyr: A Grammar of Data Manipulation. https://dplyr.tidyverse.org.